﻿Imports System
Imports CoreFX
Imports System.IO
Imports Lucene.Net
Imports Lucene.Net.Index
Imports Lucene.Net.Documents
Imports Microsoft.VisualBasic
Imports Lucene.Net.Analysis.Standard
Namespace LogSearch
    Public Class LogIndexer
        Private lngTemp, PathVar, Time, Message, User As String
        Private IndexExists, CreateIndex As Boolean
        Private IndexDocCount, I As Integer
        Private LTItm As LTIItem
        Private LTICol As LTICollection = New LTICollection
        Private IndexerThread As System.Threading.Thread
        Dim doc As Lucene.Net.Documents.Document
        Public Sub New()
            PathVar = CoreFX.API.GetDataPath() & "Logs"
            IndexerThread = New System.Threading.Thread(AddressOf LoadLogs)
            IndexerThread.Start()
        End Sub
        Private Sub LoadLogs()
            Dim Dirs As String() = Nothing
            If IO.Directory.Exists(PathVar) Then
                Dirs = System.IO.Directory.GetDirectories(PathVar)
            Else
                Exit Sub
            End If
            For Each Dir As String In Dirs
                For Each Log As String In Directory.GetFiles(Dir, "*.txt")
                    If Log.Contains("-indexed") Or Not Log.Contains("-indexed") Then
                        ''[Global].Services.Get(Of Interfaces.ILogging).AddItem("LoadLogs: Indexing File " & Dir & "\" & Path.GetFileName(Log), CoreFX.Logging.MessageType.General)
                        FileOpen(1, Log, OpenMode.Input)
                        Do Until EOF(1)
                            Input(1, lngTemp)
                            If lngTemp <> "" Then
                                LTItm = New LTIItem
                                Time = GetTime(lngTemp)
                                If Time <> "" Then lngTemp = lngTemp.Replace(Time, " ")
                                User = GetUser(lngTemp)
                                If User <> "" Then lngTemp = lngTemp.Replace(User, " ")
                                Message = lngTemp
                                LTItm.Time = RemoveSepChars(Time, "[")
                                LTItm.User = RemoveSepChars(User, "<")
                                LTItm.Message = Message
                                LTItm.LogDate = Path.GetFileNameWithoutExtension(Log)
                                LTICol.Add(LTItm)
                            End If
                        Loop
                        FileClose()
                        IndexLogFile()
                        'If File.Exists(Dir & "\" & Path.GetFileNameWithoutExtension(Log) & "-indexed" & ".txt") = True Then
                        FileOpen(1, Log, OpenMode.Input)
                        ''FileOpen(2, Dir & "\" & Path.GetFileNameWithoutExtension(Log) & "-indexed" & ".txt", OpenMode.Append)
                        'Do Until EOF(1) = True
                        ' Input(1, lngTemp)
                        ' WriteLine(2, lngTemp)
                        ' Loop
                        ' FileClose()
                        'Else
                        '    If Not Log.Contains("-indexed") Then File.Copy(Log, Dir & "\" & Path.GetFileNameWithoutExtension(Log) & "-indexed" & ".txt")
                        'End If
                        File.Delete(Log)
                        LTICol.Clear()
                    End If
                Next
            Next
            [Global].Services.Get(Of Interfaces.ILogging).AddItem("LoadLogs: Indexing Complete!", CoreFX.Logging.MessageType.General)
        End Sub
        Private Function RemoveSepChars(ByVal Message As String, ByVal SepChar As String) As String
            Dim A As String
            If IsNothing(Message) Then
                Return Nothing
            End If
            Select Case SepChar
                Case "<"
                    A = Message.Replace("<", " ")
                    A = A.Replace(">", " ")
                    Return A
                Case "["
                    A = Message.Replace("[", " ")
                    A = A.Replace("]", " ")
                    Return A
            End Select
            Return Nothing
        End Function
        Private Function GetTime(ByVal msg As String) As String
            Try
                Dim A, B As String
                A = Mid(msg, InStr(msg, "]") + 1)
                B = msg.Replace(A, " ")
                ' C = B.Replace("[", " ")
                ' C = C.Replace("]", " ")
                Return Trim(B)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function
        Private Function GetUser(ByVal Msg As String) As String
            Dim A, B As String
            A = Trim(Mid(Msg, InStr(Msg, ">") + 1))
            Try
                B = Msg.Replace(A, " ")
            Catch ex As Exception
                B = A
            End Try
            Return Trim(B)
        End Function
        Private Sub IndexLogFile()
            Dim IndexWriter As Lucene.Net.Index.IndexWriter
            If IO.Directory.Exists(PathVar & " Index") = False Then
                CreateIndex = True
            Else
                CreateIndex = False
            End If
            IndexWriter = New Lucene.Net.Index.IndexWriter(PathVar & " Index", New Lucene.Net.Analysis.Standard.StandardAnalyzer, CreateIndex)
            [Global].Services.Get(Of Interfaces.ILogging).AddItem("IndexLogFile: Indexing " & LTICol.Count & " items", CoreFX.Logging.MessageType.General)
            For Each Item As LTIItem In LTICol
                I = I + 1
                doc = New Lucene.Net.Documents.Document
                doc.Add(Field.Keyword("Date", Item.LogDate))
                If Not IsNothing(Item.User) Then doc.Add(Field.Keyword("User", Item.User))
                If Not IsNothing(Item.Message) Then doc.Add(Field.Text("Message", Item.Message))
                If Not IsNothing(Item.Time) Then doc.Add(Field.Keyword("Time", Item.Time))
                IndexWriter.AddDocument(doc)
            Next
            IndexWriter.Optimize()
            IndexWriter.Close()
        End Sub
        Public Sub SetupIndex()

        End Sub
        Public Function ReportIndexStatus() As String
            Try
                Dim A As String
                A = PathVar & " Index"
                Dim Searcher As New Lucene.Net.Search.IndexSearcher(A)
                IndexExists = True
                IndexDocCount = Searcher.MaxDoc
                Searcher.Close()
                Searcher = Nothing
                Return String.Format("The index is ready for use with {0} items.", IndexDocCount)
            Catch ex As Exception
                IndexExists = False
                Return "The index does not exist or is corrupted. Please rebuild the index."
            End Try
        End Function
    End Class
End Namespace